<?php
declare(strict_types=1);
/**
 * MineAdmin is committed to providing solutions for quickly building web applications
 * Please view the LICENSE file that was distributed with this source code,
 * For the full copyright and license information.
 * Thank you very much for using MineAdmin.
 *
 * @Author X.Mo<root@imoi.cn>
 * @Link   https://gitee.com/xmo/MineAdmin
 */

namespace Plugin\Attractions\Inspection\Repository\Inspection_exception;

use Plugin\Attractions\Inspection\Model\Inspection_exception\InspectionException;
use Hyperf\Database\Model\Builder;
use App\Repository\IRepository;
use Hyperf\Collection\Arr;

/**
 * 临时巡检 Repository类
 */
class InspectionExceptionRepository extends IRepository
{
   public function __construct(
        protected readonly InspectionException $model
    ) {}

    /**
     * 搜索处理器
     * @param Builder $query
     * @param array $params
     * @return Builder
     */
    public function handleSearch(Builder $query, array $params): Builder
    {
        
        // ID
        if (isset($params['id']) && !empty($params['id'])) {
            $query->where('id', '=', $params['id']);
        }

        // 事件类型（“故障”、“告警”）
        if (isset($params['type']) && !empty($params['type'])) {
            $query->where('type', '=', $params['type']);
        }

        // 异常时间
        if (isset($params['date']) && !empty($params['date']) && is_array($params['date']) && count($params['date']) == 2) {
            $query->whereBetween(
                'date',
                [ $params['date'][0], $params['date'][1] ]
            );
        }

        // 事件内容/描述
        if (isset($params['content']) && !empty($params['content'])) {
            $query->where('content', 'like', '%'.$params['content'].'%');
        }

        // 处理状态 1已处理 2未处理 3已接收
        if (isset($params['status']) && !empty($params['status'])) {
            $query->where('status', '=', $params['status']);
        }

        // 巡检人
        if (isset($params['inspector']) && !empty($params['inspector'])) {
            $query->where('inspector', '=', $params['inspector']);
        }

        // 巡检设备
        if (isset($params['devices']) && !empty($params['devices'])) {
            $query->where('devices', '=', $params['devices']);
        }

        // 巡检开始时间
        if (isset($params['start_time']) && !empty($params['start_time']) && is_array($params['start_time']) && count($params['start_time']) == 2) {
            $query->whereBetween(
                'start_time',
                [ $params['start_time'][0], $params['start_time'][1] ]
            );
        }

        // 巡检结束时间
        if (isset($params['end_time']) && !empty($params['end_time']) && is_array($params['end_time']) && count($params['end_time']) == 2) {
            $query->whereBetween(
                'end_time',
                [ $params['end_time'][0], $params['end_time'][1] ]
            );
        }

        // 
        if (isset($params['created_at']) && !empty($params['created_at']) && is_array($params['created_at']) && count($params['created_at']) == 2) {
            $query->whereBetween(
                'created_at',
                [ $params['created_at'][0], $params['created_at'][1] ]
            );
        }

        // 
        if (isset($params['updated_at']) && !empty($params['updated_at']) && is_array($params['updated_at']) && count($params['updated_at']) == 2) {
            $query->whereBetween(
                'updated_at',
                [ $params['updated_at'][0], $params['updated_at'][1] ]
            );
        }

        return $query;
    }
}